package app.server.project.cloud.random.db.entity;

import app.server.project.core.syndb.SynDBMgr;
import app.server.project.core.syndb.SynSpeed;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
import lombok.Data;
import org.hibernate.annotations.Comment;

import java.time.LocalDateTime;

@Data
@Entity(name = "device_base")
@TableName("device_base")
@Comment(value = "设备基础信息表")
@SynSpeed
public class DeviceBaseEntity {

    @Id
    @Column(name = "id")
    @Comment("用户主键")
    @TableId("id")
    private long id;

    @TableField("device_type_id")
    @Comment("设备类型id")
    @Column(name = "device_type_id")
    private int deviceTypeId;



    @TableField("create_time")
    @Comment("创建时间")
    @Column(name = "create_time")
    private LocalDateTime createTime;

    @Transient
    @TableField(exist = false)
    private boolean synDb = false;

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(DeviceBaseEntity::getCreateTime, this);
    }


    public void setDeviceTypeId(int deviceTypeId) {
        this.deviceTypeId = deviceTypeId;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(DeviceBaseEntity::getDeviceTypeId, this);
    }

    public static DeviceBaseEntity of(long id, int deviceTypeId) {
        var ret=new DeviceBaseEntity();
        //打开同步代码标志
        ret.setSynDb(true);
        ret.setId(id);
        ret.setCreateTime(LocalDateTime.now());
        ret.setDeviceTypeId(deviceTypeId);
        return ret;
    }

    public static DeviceBaseEntity of() {
        var ret=new DeviceBaseEntity();
        //打开同步代码标志
        return ret;
    }
}
